package com.nurt.dao.impl;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import com.nurt.dao.OrderDao;
import com.nurt.entity.OrderEntity;
import com.nurt.utils.DatabaseManager;

public class OrderDaoImpl implements OrderDao{
    private Connection conn;

    public OrderDaoImpl() {
        try {
            this.conn = DatabaseManager.getInstance().getConnection();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public List<OrderEntity> selectOrders(int userId) throws SQLException {
        String sql = "SELECT * FROM orders WHERE userId = " + userId;
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        List<OrderEntity> orders = new ArrayList<>();
        while (rs.next()) {
            OrderEntity order = new OrderEntity();
            order.setOrderId(rs.getInt("orderId"));
            order.setUserId(rs.getInt("userId"));
            order.setStatus(rs.getInt("status"));
            orders.add(order);
        }
        return orders;
    }

    public OrderEntity selectOrder(int userId) throws SQLException {
        String sql = "SELECT * FROM orders WHERE userId = " + userId;
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        if (rs.next()) {
            OrderEntity order = new OrderEntity();
            order.setOrderId(rs.getInt("orderId"));
            order.setUserId(rs.getInt("userId"));
            order.setStatus(rs.getInt("status"));
            return order;
        } else {
            return null;
        }
    }

    public List<OrderEntity> selectOrdersByStatus(int userId, int status) throws SQLException {
        String sql = "SELECT * FROM orders WHERE userId = " + userId + " AND status = " + status;
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        List<OrderEntity> orders = new ArrayList<>();
        while (rs.next()) {
            OrderEntity order = new OrderEntity();
            order.setOrderId(rs.getInt("orderId"));
            order.setUserId(rs.getInt("userId"));
            order.setStatus(rs.getInt("status"));
            orders.add(order);
        }
        return orders;
    }

    public int updateOrder(OrderEntity order) throws SQLException {
        String sql = "UPDATE orders SET userId = " + order.getUserId() + ", status = " + order.getStatus() + " WHERE orderId = " + order.getOrderId();
        Statement stmt = conn.createStatement();
        return stmt.executeUpdate(sql);
    }

    public int updateOrderStatus(int orderId, int status) throws SQLException {
        String sql = "UPDATE orders SET status = " + status + " WHERE orderId = " + orderId;
        Statement stmt = conn.createStatement();
        return stmt.executeUpdate(sql);
    }

    public int insertOrder(OrderEntity order) throws SQLException {
        String sql = "INSERT INTO orders (userId, status) VALUES (" + order.getUserId() + ", " + order.getStatus() + ")";
        Statement stmt = conn.createStatement();
        stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
        ResultSet rs = stmt.getGeneratedKeys();
        if (rs.next()) {
            return rs.getInt(1);
        } else {
            throw new SQLException("Creating order failed, no ID obtained.");
        }
    }
}